package web;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import vo.Source;
import vo.SourceCategory;
import vo.User;
import vo.UserTopic;
import business.SourceCategoryService;
import business.SourceService;
import business.UserService;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.Preparable;

@SuppressWarnings("unchecked")
public class SourceCategoryAction implements Preparable {
	private SourceCategoryService service;
	private SourceService sourceService;
	private UserService userService;

	private List<SourceCategory> sourceCategories;
	private ArrayList<String> delIds = new ArrayList<String>();

	private ArrayList<String> SelectedItem = new ArrayList<String>();// 新建source
																		// category
	private ArrayList<String> WaitSelectItem = new ArrayList<String>();// 已选择的列表

	// 分页变量
	private int curPage;
	private int totalPage;
	private int pageSize = 4;

	private List<SourceCategory> selectedSCs;
	private List<SourceCategory> nonSelectedSCs;

	private List<Source> sources;// 待选的
	private List<Source> selectedSources;// 已选的

	private SourceCategory sourceCategory;
	private long sourceCategoryId;
	private String name;

	private List<String> names;

	private User curUser;

	public String listSourceCategory() {
		System.out.println("curPage:" + curPage);
		if (curPage == 0)
			curPage = 1;

		// 查询所有的
		List<SourceCategory> tmp = service.getAllSourceCategories();
		ArrayList<SourceCategory> filters = new ArrayList<SourceCategory>();
		sourceCategories = new ArrayList<SourceCategory>();
		System.out.println("source category count:" + tmp.size());

		names = new ArrayList<String>();
		// 投影name到filters上
		for (SourceCategory sc : tmp) {
			String name = sc.getName();
			if (!names.contains(name)) {
				names.add(name);
				filters.add(sc);
			}
		}

		System.out.println("多少个类别:" + filters.size());

		int totalCount = filters.size();
		if (totalCount % pageSize == 0)
			totalPage = totalCount / pageSize;
		else
			totalPage = totalCount / pageSize + 1;
		System.out.println("totalCount / pageSize:" + totalCount / pageSize);
		int count = 0;
		for (SourceCategory sc : filters) {
			count++;
			System.out.println("count:" + count);
			if (count <= (curPage - 1) * pageSize)
				continue;
			if (count > curPage * pageSize)
				continue;
			sourceCategories.add(sc);
		}
		return Action.SUCCESS;
	}

	public String add() {
		service.addSourceCategory("hello xxx");
		return listSourceCategory();
	}

	public String deleteSourceCategory() {
		System.out.println("delete sourceCategory:");
		for (int i = 0; i < this.delIds.size(); i++) {
			System.out.println("deleted ids：" + delIds.get(i));
			long categoryId = Long.parseLong(delIds.get(i));
			SourceCategory sc=service.getById(categoryId);
			service.delete(sc);
			// 删除级联的source
			// List<Source> sources = sourceService.getByCategoryId(categoryId);
			// for (Source s : sources) {
			// sourceService.delete(s.getId());
			// }
			// 最后再删除source category
		}
		return "sourceCategoryDeleted";
	}

	// 新建分组
	public String saveSourceCategory() {
		System.out.println("form save category");
		System.out.println("selected:" + this.SelectedItem.toString());
		System.out.println("sourcec categoryId:" + sourceCategoryId);
		for (String sourceId : this.SelectedItem) {
			SourceCategory sourceCategory = new SourceCategory();
			sourceCategory.setName(name);
			sourceCategory.setSource(sourceService.getById(Integer.parseInt(sourceId)));
			service.addSourceCategory(sourceCategory);
		}
		return "saveSourceCategory";
	}

	public String newSourceCategory() {
		System.out.println("newSourceCategory");
		// 显示所有的sources
		this.sources = sourceService.getAllSources();

		return Action.SUCCESS;
	}

	// 显示分组编辑页面
	public String editSourceCategory() {
		System.out.println("editSourceCategory:" + sourceCategoryId);
		sources = new ArrayList<Source>();
		selectedSources = new ArrayList<Source>();

		// 某sourceCategoryId对应分组，所有的相同分组名，去掉重复的
		SourceCategory sourceCategory = service.getById(sourceCategoryId);
		name = sourceCategory.getName();// 被修改的分组名称
		List<SourceCategory> scs = service.getByName(name); // 相同名字的分组们,有很多很多的sources
		for (SourceCategory sc : scs) {
			long sourceId = sc.getSource().getId();
			Source tmp = sourceService.getById(sourceId);
			if (!selectedSources.contains(tmp))
				selectedSources.add(tmp);
		}

		// 所有source中，除掉本分组的
		List<Source> all = sourceService.getAllSources();
		for (Source source : all) {
			if (!selectedSources.contains(source))
				sources.add(source);
		}

		System.out.println("非分组(" + name + "):" + this.selectedSources.size());
		return Action.SUCCESS;
	}

	public String updateSourceCategory() {
		System.out.println("updateSourceCategory");
		System.out.println("name:" + name);
		System.out.println("sourceCategoryId:" + sourceCategoryId);

		// selected的列表（if source没分组，加入分组关系；否则啥都不做）
		for (String sourceId : SelectedItem) {
			// 如果source没有加入分组中，那么加入
			if (service.getByNameSourceId(name, Long.parseLong(sourceId))==null) {
				SourceCategory sourceCategory = new SourceCategory();
				sourceCategory.setName(name);
				Source source = sourceService.getById(Integer.parseInt(sourceId));
				sourceCategory.setSource(source);
				service.addSourceCategory(sourceCategory);
			}
		}

		// unselected列表(如果有分组，删除分组关系；否则啥都不做)
		for (String sourceId : WaitSelectItem) {
			// 如果source已经加入分组了，那么删除
			SourceCategory sourceCategory = service.getByNameSourceId(name, Long.parseLong(sourceId));
			if (sourceCategory!=null) {
				service.delete(sourceCategory);
				
			}
		}

		return "updateSourceCategory";
	}

	public List<SourceCategory> getSourceCategories() {
		return sourceCategories;
	}

	public void prepare() throws Exception {
		ActionContext ctx = ActionContext.getContext();
		Map session = ctx.getSession();
		String username = (String) session.get("username");
		curUser = userService.getByLoginName(username);
		System.out.println("curUser.getLoginName():" + curUser.getLoginName());
	}

	public void setSourceCategoryService(SourceCategoryService service) {
		this.service = service;
	}

	public ArrayList<String> getDelIds() {
		return delIds;
	}

	public void setDelIds(ArrayList<String> delIds) {
		this.delIds = delIds;
	}

	public void setService(SourceCategoryService service) {
		this.service = service;
	}

	public void setSourceService(SourceService sourceService) {
		this.sourceService = sourceService;
	}

	public int getCurPage() {
		return curPage;
	}

	public void setCurPage(int curPage) {
		this.curPage = curPage;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public List<SourceCategory> getSelectedSCs() {
		return selectedSCs;
	}

	public void setSelectedSCs(List<SourceCategory> selectedSCs) {
		this.selectedSCs = selectedSCs;
	}

	public List<SourceCategory> getNonSelectedSCs() {
		return nonSelectedSCs;
	}

	public void setNonSelectedSCs(List<SourceCategory> nonSelectedSCs) {
		this.nonSelectedSCs = nonSelectedSCs;
	}

	public List<Source> getSources() {
		return sources;
	}

	public void setSources(List<Source> sources) {
		this.sources = sources;
	}

	public List<Source> getSelectedSources() {
		return selectedSources;
	}

	public void setSelectedSources(List<Source> selectedSources) {
		this.selectedSources = selectedSources;
	}

	public ArrayList<String> getSelectedItem() {
		return SelectedItem;
	}

	public void setSelectedItem(ArrayList<String> selectedItem) {
		SelectedItem = selectedItem;
	}

	public void setSourceCategories(List<SourceCategory> sourceCategories) {
		this.sourceCategories = sourceCategories;
	}

	public SourceCategory getSourceCategory() {
		return sourceCategory;
	}

	public void setSourceCategory(SourceCategory sourceCategory) {
		this.sourceCategory = sourceCategory;
	}

	public long getSourceCategoryId() {
		return sourceCategoryId;
	}

	public void setSourceCategoryId(long sourceCategoryId) {
		this.sourceCategoryId = sourceCategoryId;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public List<String> getNames() {
		return names;
	}

	public void setNames(List<String> names) {
		this.names = names;
	}

	public User getCurUser() {
		return curUser;
	}

	public void setCurUser(User curUser) {
		this.curUser = curUser;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public ArrayList<String> getWaitSelectItem() {
		return WaitSelectItem;
	}

	public void setWaitSelectItem(ArrayList<String> waitSelectItem) {
		WaitSelectItem = waitSelectItem;
	}

}